#include "Settings.h"
#include <iostream>

void Settings::windowSize()
{
	setMinimumSize(Settings::winWidth, Settings::winHeight);
	resize(Settings::winWidth, Settings::winHeight);
	setWindowTitle(tr("Settings"));
}


Settings::Settings(QStringList *sl, MyWindow * mywin, QMainWindow *parent):QDialog(parent),settingsList(sl),mw(mywin)
{
	windowSize();

	QVBoxLayout *mainLayout = new QVBoxLayout;

	styleLabel     = new QLabel(tr("Theme:"));
	languageLabel  = new QLabel(tr("Language:"));

	styleCombo = new QComboBox();

	bool realstyle = false;
	foreach (QString styleName, QStyleFactory::keys())
	{
		styleCombo->addItem(styleName);
		connect(styleCombo, SIGNAL(currentIndexChanged(QString)), this, SLOT(changeStyle(QString)));

		if (!realstyle && !styleName.compare(settingsList->at(1)))
		{
			styleCombo->setCurrentIndex((QStyleFactory::keys()).indexOf(settingsList->at(1)));
			realstyle = true;
		}
	}

	// Style par defaut ou si pas valide = Plastique
	if (!realstyle)
	{
		styleCombo->setCurrentIndex((QStyleFactory::keys()).indexOf("Plastique"));
	}

	languageCombo = new QComboBox();
	languageCombo->addItem(tr("English"));
	languageCombo->addItem(tr("French"));
	connect(languageCombo, SIGNAL(currentIndexChanged(int)), this, SLOT(changeLanguage(int)));

	// Langue par defaut ou si pas valide = english
	if (settingsList->at(0).compare("FR") == 0) languageCombo->setCurrentIndex(1);
	else languageCombo->setCurrentIndex(0);

	mainLayout->addWidget(languageLabel);
	mainLayout->addWidget(languageCombo);
	mainLayout->addWidget(styleLabel);
	mainLayout->addWidget(styleCombo);

	mainLayout->addStretch(1);

	buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
	connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
	connect(buttonBox, SIGNAL(rejected()), this, SLOT(closeSettings()));

	mainLayout->addWidget(buttonBox);

	setLayout(mainLayout);
}

void Settings::accept()
{
	QString lang = (languageCombo->currentText().compare(tr("French")) == 0) ? "FR" : "EN";

	QFile file("conf.ini");
	if (file.open(QIODevice::ReadWrite | QFile::Truncate))
	{
		QTextStream out(&file);
		out << "LANG: " << lang << "\r\n";
		out << "STY: " << styleCombo->currentText();
	}

	settingsList->removeAt(1);
	settingsList->insert(1, styleCombo->currentText());

	settingsList->removeAt(0);
	settingsList->insert(0, lang);

	mw->applySettings(settingsList);
	changeLanguage(languageCombo->currentIndex());
	this->close();
}

void Settings::closeSettings()
{
	mw->applySettings(settingsList);
	this->close();
}

void Settings::changeStyle(QString stylename)
{
	QStyle *style = QStyleFactory::create(stylename);
	Q_ASSERT(style);
	QApplication::setStyle(style);
}

void Settings::defaultSettings()
{
	QFile file("conf.ini");

	if (file.open(QIODevice::ReadWrite | QFile::Truncate))
	{
		QTextStream out(&file);
		out << "LANG: English" << "\r\n";
		out << "STY: plastique" ;
	}
}


void Settings::changeLanguage(int id)
{
	if (id == 0) mw->englishLanguage();
	else if (id == 1) mw->frenchLanguage();
}

Settings::~Settings()
{
}
